#include "WebStationDataStore.h"

#if 0
struct debug_settings
{
    std::string m_file;          // log filename
    int m_level;                 // debug level
    std::set<string> m_modules;  // modules where logging is enabled
    void load(const std::string &filename);
    //void save(const std::string &filename);
};

// Loads debug_settings structure from the specified XML file
void debug_settings::load(const std::string &filename)
{
    // Create an empty property tree object
    using boost::property_tree::ptree;
    ptree pt;

    // Load the XML file into the property tree. If reading fails
    // (cannot open file, parse error), an exception is thrown.
    read_xml(filename, pt);

    // Get the filename and store it in the m_file variable.
    // Note that we construct the path to the value by separating
    // the individual keys with dots. If dots appear in the keys,
    // a path type with a different separator can be used.
    // If the debug.filename key is not found, an exception is thrown.
    m_file = pt.get<std::string>("debug.filename");

    // Get the debug level and store it in the m_level variable.
    // This is another version of the get method: if the value is
    // not found, the default value (specified by the second
    // parameter) is returned instead. The type of the value
    // extracted is determined by the type of the second parameter,
    // so we can simply write get(...) instead of get<int>(...).
    m_level = pt.get("debug.level", 0);

    // Iterate over the debug.modules section and store all found
    // modules in the m_modules set. The get_child() function
    // returns a reference to the child at the specified path; if
    // there is no such child, it throws. Property tree iterators
    // are models of BidirectionalIterator.
    BOOST_FOREACH(ptree::value_type &v,
            pt.get_child("debug.modules"))
        m_modules.insert(v.second.data());
}
#endif
#if 1
void WebStationDataStore::load(){
    using boost::property_tree::ptree;
    ptree pt;

    try {
        
        // Load the XML file into the property tree. If reading fails
        // (cannot open file, parse error), an exception is thrown.
        read_xml(m_path, pt);

        // Get the filename and store it in the m_file variable.
        // Note that we construct the path to the value by separating
        // the individual keys with dots. If dots appear in the keys,
        // a path type with a different separator can be used.
        // If the debug.filename key is not found, an exception is thrown.
        //m_file = pt.get<std::string>("debug.filename");

        // Get the debug level and store it in the m_level variable.
        // This is another version of the get method: if the value is
        // not found, the default value (specified by the second
        // parameter) is returned instead. The type of the value
        // extracted is determined by the type of the second parameter,
        // so we can simply write get(...) instead of get<int>(...).
        //m_level = pt.get("debug.level", 0);

        // Iterate over the debug.modules section and store all found
        // modules in the m_modules set. The get_child() function
        // returns a reference to the child at the specified path; if
        // there is no such child, it throws. Property tree iterators
        // are models of BidirectionalIterator.
        
    
        BOOST_FOREACH(ptree::value_type &iter, pt.get_child("WebStationDataStore")){
        //for (boost::property_tree::ptree::value_type* iter = pt.begin(); iter != pt.end(); iter++){
            if (iter.first == "item"){
                m_station_list.push_back(
                    std::shared_ptr<RadioStation> (new RadioStation(
                        iter.second.get<std::string>("name").c_str(),//get<std::string>("name")
                        iter.second.get<std::string>("address").c_str()//get<std::string>("address")
                    ))
                );
            }
        }
    }catch(...){
        LOG << "Error while reading xml file for WebStationDataStore : " << m_path << std::endl;
    }
}

void WebStationDataStore::save(){
    // Create an empty property tree object
    using boost::property_tree::ptree;
    ptree pt;

    // Put log filename in property tree
    //pt.put("debug.filename", m_file);

    // Put debug level in property tree
    //pt.put("debug.level", m_level);

    // Iterate over the modules in the set and put them in the
    // property tree. Note that the put function places the new
    // key at the end of the list of keys. This is fine most of
    // the time. If you want to place an item at some other place
    // (i.e. at the front or somewhere in the middle), this can
    // be achieved using a combination of the insert and put_own
    // functions.
    try{
        BOOST_FOREACH(const std::shared_ptr<RadioStation> rs, m_station_list)
        {
            //pt.add("debug.modules.module", name);
            //std::cout << "azer" << std::endl;
            ptree& node = pt.add("WebStationDataStore.item", "");
            //pt2.put("item.filename", m_file);
            node.put("name", rs->name);
            node.put("address", rs->address);
        }
    }catch(...){
        LOG << "Error while writting xml file for WebStationDataStore : " << m_path << std::endl;
    }

    // Write the property tree to the XML file.
    write_xml(m_path/*+std::string("_write")*/, pt);
}

#endif
